package com.course.android.ui.fragment;


import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baselib.android.utils.Logger;
import com.baselib.android.utils.ToastTool;
import com.course.android.R;
import com.course.android.ui.view.EndLessOnScrollListener;
import com.course.android.ui.view.RefreshLayout;
import com.course.model.HttpConfigBean;
import com.course.utils.web.HttpRespCallBack;

import org.apache.commons.lang.StringUtils;

import java.util.List;

public abstract class BaseNewsNetFragment extends BaseFragment implements HttpRespCallBack {
    protected RecyclerView lvView;
    protected RefreshLayout swipeView;
    protected int pageNum=1;
    protected int pageSize=20;

    boolean hasMore=false;

    @Override
    public void onReqStart(HttpConfigBean flag) {
        Logger.d("onReqStart"+flag);

        swipeView.setRefreshing(true);
    }
    @Override
    public void onReqFinish(HttpConfigBean flag) {
        Logger.d("onReqFinish"+flag);


        swipeView.setRefreshing(false);
    }
    @Override
    public void onReqFail(HttpConfigBean flag, Exception e, String errorMsg) {
        Logger.d("onReqFail"+flag);
        if (flag.isErrorToast() && StringUtils.isNotBlank(errorMsg)) {
                        ToastTool.show(getActivity(), errorMsg);
        }
    }

    public abstract RecyclerView.LayoutManager getLayoutManager();

    protected void initLv(View view) {
        super.onCreate(view);
        lvView = view.findViewById(R.id.lvView);
        swipeView = view.findViewById(R.id.swipeView);
        swipeView.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        lvView.setItemAnimator(new DefaultItemAnimator());  // 设置循环视图的动画效果
        lvView.setLayoutManager(getLayoutManager());
        if (getLayoutManager() instanceof LinearLayoutManager){
            EndLessOnScrollListener endLessOnScrollListener = new EndLessOnScrollListener((LinearLayoutManager)getLayoutManager()) {
                @Override
                public void onLoadMore(int currentPage) {
                    if (hasMore) {
                        loadNext();
                    }
                }
            };
            lvView.addOnScrollListener(endLessOnScrollListener);
        }


        swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                pageNum=1;
                loadNext();
            }
        });
    }
    public  abstract void loadNext();


    @Override
    public void onSuccess(HttpConfigBean flag, boolean page, Object o) {
        if (page) {
            if (o==null){
                onData(flag, o);
                return;
            }
            JSONObject json = (JSONObject) o;
            int total = json.getIntValue("total");
            int pageNum = json.getIntValue("pageNum");
            int pageSize = json.getIntValue("pageSize");
            onPageData(flag, pageNum, pageSize, total,
                    JSONArray.parseArray(json.getJSONArray("data").toJSONString()
                            , flag.getClas()));
            hasMore = total>pageNum*pageSize;
            return;
        }
        if (flag.getClas().equals(String.class)) {
            onData(flag, o.toString());
            return;
        }
        onData(flag,o==null?null: JSONObject.parseObject(o.toString(), flag.getClas()));
    }

    public abstract <T> void onData(HttpConfigBean flag, T o);

    public abstract <T> void onPageData(HttpConfigBean flag, int pageNum, int pageSize, int total, List<T> array);


}
